package com.ops.service.ns.system.beans;

import java.util.List;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.ops.schema.ns.system.OPSRecordAuditInfo;
import com.ops.schema.ns.system.OPSUserSessions;
import com.ops.schema.ns.system.SearchUserSessionsRequest;
import com.ops.schema.ns.system.SearchUserSessionsResponse;
import com.ops.service.ns.system.client.OnlinePayrollSystemServiceClient;
import com.ops.service.ns.system.exception.ServiceException;


public class UserSessionMBean {
	
	private String userId;
	private String sessionStart;
	private String sessionEnd;
	private List<OPSUserSessions> opsUserSessionlst;
	
	public void searchUserSessions() {
		System.out.println("Inside searchRules");
		SearchUserSessionsRequest userRequest = new SearchUserSessionsRequest();
		userRequest.setUserId(userId);
		userRequest.setSessionStartTime(sessionStart);
		userRequest.setSessionEndTime(sessionEnd);
		OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
		uditInfo.setUpdatedBy(getuserId());
		uditInfo.setUpdatedSession(getSessionId());
		uditInfo.setAction("S");
		uditInfo.setObjectType(getMenuId());
		userRequest.setOPSRecordAuditInfo(uditInfo);
		try {
			SearchUserSessionsResponse response = OnlinePayrollSystemServiceClient
					.searchUserSessions(userRequest);
			setOpsUserSessionlst(response.getOPSUserSessions());

		} catch (ServiceException e) {
			e.printStackTrace();
		}

	}

	public void resetUserSessions() {
		setUserId(null);
		setSessionEnd(null);
		setSessionStart(null);
	}

	private String getSessionId() {

		FacesContext fCtx = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) fCtx.getExternalContext()
				.getSession(false);
		return session.getId();
	}

	private String getuserId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("UserId").toString();
	}
	private String getMenuId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("MenuId").toString();
	}
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getSessionStart() {
		return sessionStart;
	}

	public void setSessionStart(String sessionStart) {
		this.sessionStart = sessionStart;
	}

	public String getSessionEnd() {
		return sessionEnd;
	}

	public void setSessionEnd(String sessionEnd) {
		this.sessionEnd = sessionEnd;
	}

	public List<OPSUserSessions> getOpsUserSessionlst() {
		return opsUserSessionlst;
	}

	public void setOpsUserSessionlst(List<OPSUserSessions> opsUserSessionlst) {
		this.opsUserSessionlst = opsUserSessionlst;
	}

	
	
}
